package ACM;

import java.io.*;

/**
 * @author way
 * @create 2025-03-28 19:37
 */
public class ELM_1 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        // 读取输入参数
        int k = Integer.parseInt(br.readLine());
        String s = br.readLine();
        int n = s.length();
        int q = Integer.parseInt(br.readLine());

        // 预处理前缀和数组
        long[] prefixSum = new long[n + 1];
        for (int i = 1; i <= n; i++) {
            char c = s.charAt(i - 1);
            prefixSum[i] = prefixSum[i - 1] + (i % k == 0 ? 1 : String.valueOf((int) c).length());
        }

        // 处理每个查询
        while (q-- > 0) {
            String[] lr = br.readLine().split(" ");
            long l = Long.parseLong(lr[0]);
            long r = Long.parseLong(lr[1]);

            // 边界检查
            if (l > prefixSum[n] || l > r) {
                bw.write("\n");
                continue;
            }
            r = Math.min(r, prefixSum[n]);

            // 二分查找定位区间
            int iStart = findIndex(prefixSum, l);
            int iEnd = findIndex(prefixSum, r);
            long startOffset = l - prefixSum[iStart - 1] - 1;
            long endOffset = r - prefixSum[iEnd - 1] - 1;

            // 构建结果
            StringBuilder sb = new StringBuilder();
            int remain = (int) (r - l + 1);
            for (int i = iStart; i <= iEnd && remain > 0; i++) {
                char c = s.charAt(i - 1);
                String transformed = (i % k == 0) ?
                        flipCase(c) :
                        String.valueOf((int) c);

                // 计算截取范围
                int start = (i == iStart) ? (int) startOffset : 0;
                int end = (i == iEnd) ? (int) endOffset : transformed.length() - 1;
                end = Math.min(start + remain - 1, end);

                if (start <= end) {
                    sb.append(transformed, start, end + 1);
                    remain -= (end - start + 1);
                }
            }
            bw.write(sb.toString() + "\n");
        }
        bw.flush();
    }

    // 二分查找工具函数
    private static int findIndex(long[] arr, long target) {
        int left = 0, right = arr.length - 1, res = arr.length;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] >= target) {
                res = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return res;
    }

    // 大小写翻转
    private static String flipCase(char c) {
        return Character.isUpperCase(c) ?
                String.valueOf(Character.toLowerCase(c)) :
                String.valueOf(Character.toUpperCase(c));
    }
}
